package com.lottery.shadowbd.ui.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lottery.shadowbd.R;
import com.lottery.shadowbd.ui.fragment.LotAnalysisFragment;
import com.lottery.shadowbd.ui.fragment.LotSettingFragment;
import com.lottery.shadowbd.ui.fragment.LotTrendFragment;
import com.lottery.shadowbd.ui.fragment.LotteryDrawFragment;
import com.lottery.shadowbd.ui.fragment.LotteryInfoFragment;

public class HomeActivity extends BaseActivity {

    private RelativeLayout rl_main_nav1;
    private RelativeLayout rl_main_nav2;
    private RelativeLayout rl_main_nav3;
    private RelativeLayout rl_main_nav4;

    private ImageView iv_main_nav1;
    private ImageView iv_main_nav2;
    private ImageView iv_main_nav3;
    private ImageView iv_main_nav4;

    private TextView tv_main_nav1;
    private TextView tv_main_nav2;
    private TextView tv_main_nav3;
    private TextView tv_main_nav4;

    private FragmentTransaction mTransaction;

    private LotteryDrawFragment mLotteryDrawFragment;
    private LotAnalysisFragment mLotAnalysisFragment;
    private LotTrendFragment mLotTrendFragment;
    private LotSettingFragment mLotSettingFragment;

    private Fragment mCurrentFragment;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_home);

        rl_main_nav1 = (RelativeLayout) findViewById(R.id.rl_main_nav1);
        rl_main_nav2 = (RelativeLayout) findViewById(R.id.rl_main_nav2);
        rl_main_nav3 = (RelativeLayout) findViewById(R.id.rl_main_nav3);
        rl_main_nav4 = (RelativeLayout) findViewById(R.id.rl_main_nav4);

        iv_main_nav1 = (ImageView) findViewById(R.id.iv_main_nav1);
        iv_main_nav2 = (ImageView) findViewById(R.id.iv_main_nav2);
        iv_main_nav3 = (ImageView) findViewById(R.id.iv_main_nav3);
        iv_main_nav4 = (ImageView) findViewById(R.id.iv_main_nav4);

        tv_main_nav1 = (TextView) findViewById(R.id.tv_main_nav1);
        tv_main_nav2 = (TextView) findViewById(R.id.tv_main_nav2);
        tv_main_nav3 = (TextView) findViewById(R.id.tv_main_nav3);
        tv_main_nav4 = (TextView) findViewById(R.id.tv_main_nav4);
    }

    @Override
    protected void initData() {
        mContext = this;

        initFragment();
    }

    @Override
    protected void initListener() {
        rl_main_nav1.setOnClickListener(this);
        rl_main_nav2.setOnClickListener(this);
        rl_main_nav3.setOnClickListener(this);
        rl_main_nav4.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.rl_main_nav1:
                toLotteryDraw();
                break;
            case R.id.rl_main_nav2:
                toLotteryTrend();
                break;
            case R.id.rl_main_nav3:
                toLotAnalysis();
                break;
            case R.id.rl_main_nav4:
                toLotterySet();
                break;
            default:
                break;
        }
    }

    //初始化Fragment
    private void initFragment() {
        mTransaction = getSupportFragmentManager().beginTransaction();

        mLotteryDrawFragment = new LotteryDrawFragment();
        mCurrentFragment = mLotteryDrawFragment;

        mTransaction.add(R.id.frame_lottery, mLotteryDrawFragment);
        mTransaction.commit();
    }

    //开奖
    private void toLotteryDraw() {
        if (mCurrentFragment == mLotteryDrawFragment) {
            return;
        }

        iv_main_nav1.setImageResource(R.mipmap.ic_launcher);
        tv_main_nav1.setTextColor(ContextCompat.getColor(mContext, R.color.colorMain));

        iv_main_nav2.setImageResource(R.mipmap.ic_launcher);
        tv_main_nav2.setTextColor(ContextCompat.getColor(mContext, R.color.mainText));

        iv_main_nav3.setImageResource(R.mipmap.ic_launcher);
        tv_main_nav3.setTextColor(ContextCompat.getColor(mContext, R.color.mainText));

        iv_main_nav4.setImageResource(R.mipmap.ic_launcher);
        tv_main_nav4.setTextColor(ContextCompat.getColor(mContext, R.color.mainText));

        if (mLotteryDrawFragment == null) {
            mLotteryDrawFragment = new LotteryDrawFragment();
        }

        switchFragment(mLotteryDrawFragment);
    }

    //趋势图
    private void toLotteryTrend() {
        if (mCurrentFragment == mLotTrendFragment) {
            return;
        }

        iv_main_nav1.setImageResource(R.mipmap.ic_launcher);
        tv_main_nav1.setTextColor(ContextCompat.getColor(mContext, R.color.mainText));

        iv_main_nav2.setImageResource(R.mipmap.ic_launcher);
        tv_main_nav2.setTextColor(ContextCompat.getColor(mContext, R.color.colorMain));

        iv_main_nav3.setImageResource(R.mipmap.ic_launcher);
        tv_main_nav3.setTextColor(ContextCompat.getColor(mContext, R.color.mainText));

        iv_main_nav4.setImageResource(R.mipmap.ic_launcher);
        tv_main_nav4.setTextColor(ContextCompat.getColor(mContext, R.color.mainText));

        if (mLotTrendFragment == null) {
            mLotTrendFragment = new LotTrendFragment();
        }

        switchFragment(mLotTrendFragment);
    }

    //统计分析
    private void toLotAnalysis() {
        if (mCurrentFragment == mLotAnalysisFragment) {
            return;
        }

        iv_main_nav1.setImageResource(R.mipmap.ic_launcher);
        tv_main_nav1.setTextColor(ContextCompat.getColor(mContext, R.color.mainText));

        iv_main_nav2.setImageResource(R.mipmap.ic_launcher);
        tv_main_nav2.setTextColor(ContextCompat.getColor(mContext, R.color.mainText));

        iv_main_nav3.setImageResource(R.mipmap.ic_launcher);
        tv_main_nav3.setTextColor(ContextCompat.getColor(mContext, R.color.colorMain));

        iv_main_nav4.setImageResource(R.mipmap.ic_launcher);
        tv_main_nav4.setTextColor(ContextCompat.getColor(mContext, R.color.mainText));

        if (mLotAnalysisFragment == null) {
            mLotAnalysisFragment = new LotAnalysisFragment();
        }

        switchFragment(mLotAnalysisFragment);
    }

    //设置
    private void toLotterySet() {
        if (mCurrentFragment == mLotSettingFragment) {
            return;
        }

        iv_main_nav1.setImageResource(R.mipmap.ic_launcher);
        tv_main_nav1.setTextColor(ContextCompat.getColor(mContext, R.color.mainText));

        iv_main_nav2.setImageResource(R.mipmap.ic_launcher);
        tv_main_nav2.setTextColor(ContextCompat.getColor(mContext, R.color.mainText));

        iv_main_nav3.setImageResource(R.mipmap.ic_launcher);
        tv_main_nav3.setTextColor(ContextCompat.getColor(mContext, R.color.mainText));

        iv_main_nav4.setImageResource(R.mipmap.ic_launcher);
        tv_main_nav4.setTextColor(ContextCompat.getColor(mContext, R.color.colorMain));

        if (mLotSettingFragment == null) {
            mLotSettingFragment = new LotSettingFragment();
        }

        switchFragment(mLotSettingFragment);
    }

    //切换Fragment
    private void switchFragment(Fragment targetFragment) {
        if (mCurrentFragment != targetFragment) {
            mTransaction = getSupportFragmentManager().beginTransaction();

            if (targetFragment.isAdded()) {
                mTransaction.hide(mCurrentFragment).show(targetFragment);
            } else {
                mTransaction.hide(mCurrentFragment).add(R.id.frame_lottery, targetFragment);
            }

            mCurrentFragment = targetFragment;

            mTransaction.commit();
        }
    }
}
